from flask import url_for, current_app

__author__ = 'hill'

import unittest
import os
from mbsh import create_app
from mbsh.api.device_api import *


class DeviceApiTestCase(unittest.TestCase):
    def setUp(self):
        config_name = os.getenv('FLASK_CONFIG') or 'default'
        app = create_app(config_name)
        app.config['SERVER_NAME'] = "test"
        app.app_context().push()
        self.app = app
        self.app.register_blueprint(api_app)
        self.client = app.test_client()

    def tearDown(self):
        print('tear down')
        # os.unlink(mbsh.app.config['DATABASE'])

    def test_fetch_reports(self):
        result = fetch_reports("test")
        print(result)
        self.assertIsNotNone(json.loads(result))

    def test_fetch_report(self):
        result = fetch_report("test", None)
        data = json.loads(result).get("list")
        self.assertIsNotNone(data)

        response = self.client.get(url_for("api_app.fetch_report", device_id="test", case_id="not_exists"), data={})
        data = json.loads(response.data).get("list")
        self.assertFalse(data)

    def test_fetch_top_reports(self):

        response = self.client.get(url_for("api_app.fetch_top_reports"), data={})
        data = json.loads(response.data).get("list")
        print(data)
        self.assertIsNotNone(data)

    def test_fetch_latest_report(self):
        response = self.client.get(url_for("api_app.fetch_latest_report", device_id="test"), data={})
        result = response.data
        data = json.loads(result).get("list")
        self.assertIsNotNone(data)

    def test_detect_area(self):
        response = self.client.post(url_for("api_app.detect_img_area"), data={})
        print(response)
        data = json.loads(response.data)
        self.assertIsNotNone(data)
